﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Samples.IHandlers;
using Microsoft.VisualStudio.IntelliTrace;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;

namespace Microsoft.VisualStudio.Samples.ConsoleWriters
{
    public class MetadataChainConsoleWriter : ChainConsoleWriter, MetadataChainHandler
    {
        public void MetadataEvent(IntelliTraceEvent ev)
        {
            MetadataEvent metadataEvent = ev as MetadataEvent;
            Debug.Assert(metadataEvent != null);

            NameValueCollection eventProperties = new NameValueCollection();
            eventProperties.Add("Mvid", metadataEvent.Mvid.ToString());
            eventProperties.Add("Metadata blob length", metadataEvent.MetadataBlob.Count.ToString(CultureInfo.CurrentCulture));
            for (int i = 0; i < metadataEvent.DebugDirectories.Count; i++)
            {
                eventProperties.Add(string.Format(CultureInfo.CurrentCulture, "Debug directory ({0})", i), FormatBlob(metadataEvent.DebugDirectories[i].Directory));
                eventProperties.Add(string.Format(CultureInfo.CurrentCulture, "Debug directory data length ({0})", i), metadataEvent.DebugDirectories[i].Data.Count.ToString(CultureInfo.CurrentCulture));
            }

            DumpProperties(eventProperties);
        }
    }
}
